package spider.impl;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import spider.NovelSiteEnum;
import spider.util.NovelSpiderHttpGet;
import spider.util.NovelSpiderUtil;

/**
 * created by yb
 * on 2017/8/9.
 **/
public abstract class AbstractSpider {

//    protected String craw(String url) throws Exception{
//        // 返回一个 CloseableHttpClient 实体   可以自动释放
//        try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//            CloseableHttpResponse httpResponse = httpClient.execute(new NovelSpiderHttpGet(url))){
//            return EntityUtils.toString(httpResponse.getEntity(), NovelSpiderUtil.getContext(NovelSiteEnum.getNovelSiteEnumByUrl(url)).get("charset"));   // 设置编码方式
//        }catch (Exception  e){
//            throw new RuntimeException(e);
//        }
//    }
    /**
     * 抓取指定小说网站的内容
     * @param url  指定的网址
     * @param charset 所需要的字符集
     * @return  网页源代码
     * @throws Exception
     */
    protected String craw(String url,String charset) throws Exception{
        // 返回一个 CloseableHttpClient 实体   可以自动释放
        try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();
            CloseableHttpResponse httpResponse = httpClient.execute(new NovelSpiderHttpGet(url))){
            return EntityUtils.toString(httpResponse.getEntity(), NovelSpiderUtil.getContext( NovelSiteEnum.getNovelSiteEnumByUrl(url)).get(charset));   // 设置编码方式
        }catch (Exception  e){
            throw new RuntimeException(e);
        }
    }
}
